package io.bdmc.common.http;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.bdmc.common.utils.CommonUtil;

@Service
public class HttpAPIService {

    public static final Logger logger = LoggerFactory.getLogger((HttpAPIService.class));
    private static String ROOTURL = "http://api.3jyun.com";
    private static String DEVICEID = "rms-169-admin";
    private static String MONITORID = "169";
    private static String COMMANDPATH = "/third/nb/north/command";
    private static String DEVDETAIL = "/third/rms/aaa/devDetail";
    private static String DELETEDEVICE = "/third/rms/device/monitor";
    private static String DEVICESECRET = "88f430c0722c7405c6a68a569ab32f2e";

    @Autowired
    private HttpClientUtil _httpClient;

    /**
     * 控制设备指令，消音等
     * 
     * @param deviceId 设备id
     * @param command  命令 “5859”： 是类型为'CTNBSmokeSensor'的设备的远程消音指令
     * @return
     * @throws Exception
     */
    public HttpResult doGetCommand(String deviceId, String command) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("deviceId", deviceId);
        map.put("command", command);
        String path = _httpClient.buildGetParams(COMMANDPATH, map);
        String timestamp = getCurrentTimeStamp();
        String sign = getSignature(path, timestamp);
        logger.info(ROOTURL + path);
        HttpResult re = _httpClient.doGet(ROOTURL + path, assemblyHeader(sign, timestamp));
        logger.info("HttpResult:" + re.getCode()+","+re.getBody());
        return re;
    }

    /**
     * 获取单个设备信息
     * 
     * @param deviceId 设备id
     * @return
     * @throws Exception
     */
    public HttpResult doGetDevDetail(String deviceId) throws Exception {
        String url = ROOTURL + DEVDETAIL + "/" + deviceId;
        String timestamp = getCurrentTimeStamp();
        String sign = getSignature(DEVDETAIL + "/" + deviceId, timestamp);
        return _httpClient.doGet(url, assemblyHeader(sign, timestamp));
    }

    /**
     * 删除单个设备
     * 
     * @param deviceId 设备id
     * @return
     * @throws Exception
     */
    public HttpResult doDeleteDev(String deviceId) throws Exception {
        String url = ROOTURL + DELETEDEVICE + "/" + deviceId;
        String timestamp = getCurrentTimeStamp();
        String sign = getSignature(DELETEDEVICE + "/" + deviceId, timestamp);
        return _httpClient.doGet(url, assemblyHeader(sign, timestamp));
    }

    private static String getCurrentTimeStamp() {
        return Calendar.getInstance().getTimeInMillis() + "";
    }

    /**
     * 组装头部信息
     * 
     * @param headers
     * @return
     */
    private static Header[] assemblyHeader(String sign, String timestamp) {
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("DeviceId", DEVICEID);
        headers.put("MonitorId", MONITORID);
        headers.put("Timestamp", timestamp);
        headers.put("Signature", sign);
        headers.put("Content-Type", "application/json;charset=UTF-8");
        Header[] allHeader = new BasicHeader[headers.size()];
        int i = 0;
        for (String str : headers.keySet()) {
            allHeader[i] = new BasicHeader(str, headers.get(str));
            i++;
        }
        return allHeader;
    }

    private String getSignature(String path, String timestamp) {
        return CommonUtil.MD5(path + timestamp + DEVICESECRET).toLowerCase();
    }
    
}
